package org.example.hadoop.service.publishYearFrequency;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author wwy
 * @date 2020-11-10 18:56
 */
public class PublishYearReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    /**
     * 查询1900年到2020年的数据
     * k2       v2
     * 2012      1,1,1
     *
     * k3       v3
     * 2012      3
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        String key1 = key.toString();
        int yearInt = Integer.parseInt(key1);
        //此处暂用魔鬼数字，比较知意
        if(yearInt >= 1900 && yearInt <= 2020){
            context.write(key,new IntWritable(sum));
        }

    }
}
